package com.myjob.serviceImpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.myjob.common.DubboConfig;
import com.myjob.config.MjServiceName;
import com.myjob.dto.UserDto;
import com.myjob.entity.*;
import com.myjob.enums.RedisEnums;
import com.myjob.handler.HrHandler;
import com.myjob.mapper.BaseInfoMapper;
import com.myjob.mapper.UserMapper;
import com.myjob.response.Result;
import com.myjob.service.BaseInfoService;
import com.myjob.utils.RedisUtil;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenyuan
 * @date 2021/1/17 11:52
 */
@Service
@Slf4j
public class BaseInfoServiceImpl implements BaseInfoService {
	@Autowired
	private BaseInfoMapper baseInfoMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private RedisUtil redisUtil;

	@Reference(group = MjServiceName.COMPANY, version = DubboConfig.VERSION)
	private HrHandler hrHandler;

	/**
	 * 按照用户 Id 获取
	 *
	 * @param userId 用户编号
	 * @return
	 */
	@Override
	public Result selectBaseInfoById(String userId) {
		Result result = null;
		if (UtilValidate.isEmpty(userId)) {
			return Result.warn("用户 id 为空");
		}
		UserDto userDto = new UserDto();
		Hr hr = hrHandler.selectOneHr(userId);
		User user = userMapper.selectById(userId);
		if (UtilValidate.isEmpty(user)) {
			return Result.failed("查询基本信息出错");
		}else {
			userDto.setUserId(user.getUserId())
					.setUserName(user.getUserName())
					.setPhone(user.getPhone())
					.setEmail(user.getEmail())
					.setAccountStatus(user.getAccountStatus())
					.setRole(user.getRole())
					.setPhoto(user.getPhoto());
		}

		if (UtilValidate.isNotEmpty(hr)) {
			userDto.setCompanyName(hr.getCompany().getCompanyName())
					.setHrStatus(hr.getHrStatus())
					.setDuty(hr.getDuty());
		}

		result = Result.succeed(userDto, "查询成功");
		return result;
	}

	/**
	 * 修改 基本信息 baseInfo
	 *
	 * @param userDto 基本信息
	 * @return
	 */
	@Override
	public Result updateBaseInfoById(UserDto userDto) {
		Result result = null;
		User user = new User();

		if (UtilValidate.isEmpty(userDto)) {
			return Result.warn("基本信息为空");
		}
		String photo = (String) redisUtil.get(RedisEnums.KEY_IN_REDIS.USER_PHOTO.getValue()
				+ userDto.getUserId() + ".png");


		if (UtilValidate.isNotEmpty(photo)) {
			user.setPhoto(photo);
		}
		// 修改 User
		user.setUserId(userDto.getUserId())
				.setUserName(userDto.getUserName())
				.setEmail(userDto.getEmail())
				.setPhone(userDto.getPhone());
		int updateUser = userMapper.updateById(user);

		int update = 1;
		if (UtilValidate.isNotEmpty(userDto.getCompanyName())){
			Hr hr = new Hr();
			hr.setUserId(userDto.getUserId());
			hr.setEmail(userDto.getEmail());
			hr.setPhone(userDto.getPhone());
			hr.setDuty(userDto.getDuty());
			update = hrHandler.updateHr(hr);
		}
		if (update <= 0 || updateUser <= 0) {
			return Result.failed("修改基本信息出错");
		}

		result = Result.succeed("修改成功");
		return result;
	}

	/**
	 * 新增 基本信息 baseInfo
	 *
	 * @param baseInfo
	 * @return
	 */
	@Override
	public Result insertBaseInfo(BaseInfo baseInfo) {
		Result result = null;
		if (UtilValidate.isEmpty(baseInfo)) {
			return Result.warn("基本信息为空");
		}

		int insert = baseInfoMapper.insert(baseInfo);
		if (insert <= 0) {
			return Result.failed("添加基本信息出错");
		}

		result = Result.succeed("添加成功");
		return result;
	}
}
